<html>
    <head>
         <title>简易计算机</title>
    </head>
    <body>
            <table border="1" width="400px" height="400px">
                <caption ><h1>简易计算器</h1></caption>
                <tr>
                    <th colspan="4">
                        <input type="text" name="" id="txtResult" readonly>
                    </th>                  
                </tr>
                <tr>
                    <th>
                        <input type="button" value="7" onclick="doInputNumber(7)">
                    </th>
                    <th>
                        <input type="button" value="8" onclick="doInputNumber(8)">
                    </th>
                    <th>
                        <input type="button" value="9" onclick="doInputNumber(9)">
                    </th>
                    <th>
                        <input type="button" value="+" onclick="doInputOperator(this.value)">
                    </th>
                </tr>
                <tr>
                    <th>
                        <input type="button" value="4" onclick="doInputNumber(4)">
                    </th>
                    <th>
                        <input type="button" value="5" onclick="doInputNumber(5)">
                    </th>
                    <th>
                        <input type="button" value="6" onclick="doInputNumber(6)">
                    </th>
                    <th>
                        <input type="button" value="-" onclick="doInputOperator(this.value)">
                    </th>
                </tr>
                <tr>
                    <th>
                        <input type="button" value="1" onclick="doInputNumber(1)">
                    </th>
                    <th>
                        <input type="button" value="2" onclick="doInputNumber(2)">
                    </th>
                    <th>
                        <input type="button" value="3" onclick="doInputNumber(3)">
                    </th>
                    <th>
                        <input type="button" value="*" onclick="doInputOperator(this.value)">
                    </th>
                </tr>
                <tr>
                    <th>
                        <input type="button" value="0" onclick="doInputNumber(0)">
                    </th>
                    <th colspan="2">
                        <input type="button" value="=" onclick="doInputEqual(this.value)">
                    </th>
                    <th>
                        <input type="button" value="/"   onclick="doInputOperator(this.value) ">
                    </th>
                </tr>
            </table>
    </body>
</html>
<script>
    var operator;//操作符
    var num1="",num2="";//操作数
    var operFlag=false;
    function doInputNumber(x){
        if(operFlag) num2=num2+x;
        else num1=num1+x;
        var obj=document.getElementById("txtResult");
        obj.value=obj.value+x;
    }
    function doInputOperator(x){
        operFlag=true;
        operator=x;
        var obj=document.getElementById("txtResult");
        num1=obj.value;
        obj.value=obj.value+x;
    } 
    function doInputEqual(x){
        var result;
        if(operator=="+") result=parseInt(num1)+parseInt(num2);
        else if(operator=="-")  result=parseInt(num1)-parseInt(num2);
        else if(operator=="*") result=parseInt(num1)*parseInt(num2);
        else if(operator=="/") result=parseInt(num1)/parseInt(num2);
        var obj=document.getElementById("txtResult");
        obj.value=obj.value+"="+result;
    }
</script>